﻿/*
 * File: Rules.cs
 * Author: Gourav Das
 * Purpose: Class for Holding and Manipulating Rules
 * Created: May 02 2011
 * Copyright: Gourav Das, 2011
 */

using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Xml;
using System.IO;

namespace _22yards.Core
{
    public class PowerPlays
    {
        public string Name { get; set; }
        public short PowerPlaySequence { get; set; }
        public short OversInPP { get; set; }
        public short MinCloseCatchers { get; set; }
        public short MinInnerCircleFielders { get; set; }
        public short MaxOutFielders { get; set; }
    }

    public class UDRS
    {
        public short ReviewsPerTeam { get; set; }
    }

    public class DrinksBreak
    {
        public short BreakAtOverNum { get; set; }
    }

    public class BallChange
    {
        public short BallChangeAtOverNum { get; set; }
    }

    public class Rules
    {
        public short ID { get; private set; }
        public string Name { get; private set; }
        public short BaseMatchType { get; set; }
        public short LengthOfMatchInDays { get; private set; }
        public short NumInningsPerTeam { get; private set; }
        public bool InningsLengthDeterminate { get; private set; } //Yes for 2020, 5050 etc...False for Test, 3 day matches etc
        public short OversPerDay { get; private set; }
        public short OversPerInnings { get; set; }
        public short NumOfPlayersPerTeam { get; private set; }
        public bool FieldRestrictions { get; private set; }
        public short MaxBehindSquareOnLeg { get; set; }
        public List<PowerPlays> PowerPlayList
        {
            get ; 
            private set ;
        }

        public short MaxOversPerBowler { get; private set; }
        public UDRS DRS { get; private set; }
        public List<DrinksBreak> drinksBreak { get; private set; }
        public List<BallChange> ballChange { get; private set; }

        public bool DrinksBreakEnabled { get; private set; }
        public bool DRSEnabled { get; private set; }
        public bool BallChangeEnabled { get; private set; }

        public Rules(string _name)
        {
            string FullPath = string.Empty;

            FullPath = @"22yardsContent\Core\Rules\" + _name + ".xml";

            XmlTextReader xmlReader;

            try
            {
                xmlReader = new XmlTextReader(FullPath);
            }
            catch (DirectoryNotFoundException ex)
            {
                throw new ArgumentException();
            }
            catch (FileNotFoundException ex)
            {
                throw new ArgumentException();
            }

            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (xmlReader.Name == "RULE")
                        {
                            Name = xmlReader.GetAttribute("name");
                            ID = Convert.ToInt16(xmlReader.GetAttribute("id"));
                            BaseMatchType = Convert.ToInt16(xmlReader.GetAttribute("type"));
                            break;
                        }

                        if (xmlReader.Name == "DAYS")
                        {
                            LengthOfMatchInDays = Convert.ToInt16(xmlReader.GetAttribute("value"));
                            break;
                        }
                        if (xmlReader.Name == "INNINGSPERTEAM")
                        {
                            NumInningsPerTeam = Convert.ToInt16(xmlReader.GetAttribute("value"));
                            break;
                        }
                        if (xmlReader.Name == "FIXEDLENGTHINNINGS")
                        {
                            InningsLengthDeterminate = Convert.ToBoolean(xmlReader.GetAttribute("value"));
                            if (InningsLengthDeterminate)
                            {
                                OversPerInnings = Convert.ToInt16(xmlReader.GetAttribute("number"));
                                OversPerDay = (short)(OversPerInnings * 2);
                            }
                            else
                            {
                                OversPerDay = Convert.ToInt16(xmlReader.GetAttribute("number"));
                                OversPerInnings = (short)(OversPerDay * LengthOfMatchInDays);
                            }
                                
                            break;
                        }
                        if (xmlReader.Name == "PLAYERS")
                        {
                            NumOfPlayersPerTeam = Convert.ToInt16(xmlReader.GetAttribute("value"));
                            break;
                        }
                        if (xmlReader.Name == "FIELDRESTRICTIONS")
                        {
                            FieldRestrictions = Convert.ToBoolean(xmlReader.GetAttribute("value"));
                            if (FieldRestrictions)
                            {
                                MaxBehindSquareOnLeg = Convert.ToInt16(xmlReader.GetAttribute("maxbehindsquareonleg"));
                                PowerPlayList = new List<PowerPlays>();

                                while (xmlReader.Read() && (xmlReader.Name != "FIELDRESTRICTIONS" || xmlReader.NodeType != XmlNodeType.EndElement))
                                {
                                    switch (xmlReader.NodeType)
                                    {
                                        case XmlNodeType.Element:
                                            if (xmlReader.Name == "POWERPLAY")
                                            {
                                                PowerPlays ppItem= new PowerPlays();
                                                ppItem.Name = xmlReader.GetAttribute("name");
                                                ppItem.PowerPlaySequence = Convert.ToInt16(xmlReader.GetAttribute("value"));
                                                ppItem.OversInPP = Convert.ToInt16(xmlReader.GetAttribute("overs"));
                                                ppItem.MinCloseCatchers = Convert.ToInt16(xmlReader.GetAttribute("closecatchmin"));
                                                ppItem.MinInnerCircleFielders = Convert.ToInt16(xmlReader.GetAttribute("innercirclemin"));
                                                ppItem.MaxOutFielders = Convert.ToInt16(xmlReader.GetAttribute("outfield"));
                                        
                                                PowerPlayList.Add(ppItem);
                                            }
                                            break;
                                    }

                                }
                            }
                            break;
                        }

                        if (xmlReader.Name == "MAXBOWLEROVERS")
                        {
                            MaxOversPerBowler = Convert.ToInt16(xmlReader.GetAttribute("value"));
                            break;
                        }

                        if (xmlReader.Name == "DRINKS")
                        {
                            
                            DrinksBreakEnabled = Convert.ToBoolean(xmlReader.GetAttribute("value"));
                            int val = Convert.ToInt32(xmlReader.GetAttribute("number"));
                            if (DrinksBreakEnabled)
                            {
                                drinksBreak = new List<DrinksBreak>(val);

                                while (xmlReader.Read() && (xmlReader.Name != "DRINKS" || xmlReader.NodeType != XmlNodeType.EndElement))
                                {
                                    switch (xmlReader.NodeType)
                                    {
                                        case XmlNodeType.Element:
                                            if (xmlReader.Name == "OVER")
                                            {
                                                DrinksBreak drBr = new DrinksBreak();
                                                drBr.BreakAtOverNum = Convert.ToInt16(xmlReader.GetAttribute("value"));

                                                drinksBreak.Add(drBr);
                                            }
                                            break;
                                    }

                                }
                            }
                            break;
                        }

                        if (xmlReader.Name == "BALLCHANGE")
                        {
                            BallChangeEnabled = Convert.ToBoolean(xmlReader.GetAttribute("value"));
                            int val = Convert.ToInt32(xmlReader.GetAttribute("number"));
                            if (BallChangeEnabled)
                            {
                                ballChange = new List<BallChange>(val);

                                while (xmlReader.Read() && (xmlReader.Name != "BALLCHANGE" || xmlReader.NodeType != XmlNodeType.EndElement))
                                {
                                    switch (xmlReader.NodeType)
                                    {
                                        case XmlNodeType.Element:
                                            if (xmlReader.Name == "OVER")
                                            {
                                                BallChange bChg = new BallChange();
                                                bChg.BallChangeAtOverNum = Convert.ToInt16(xmlReader.GetAttribute("value"));

                                                ballChange.Add(bChg);
                                            }
                                            break;
                                    }

                                }
                            }
                            break;
                        }

                        if (xmlReader.Name == "DRS")
                        {
                            DRSEnabled = Convert.ToBoolean(xmlReader.GetAttribute("value"));
                            if (DRSEnabled)
                            {
                                DRS = new UDRS();
                                DRS.ReviewsPerTeam = Convert.ToInt16(xmlReader.GetAttribute("number"));
                            }
                            break;
                        }
                        break;
                }
            }

        }

    }
}
